package com.example.com.myapplication.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.com.myapplication.R;
import com.megvii.facetrack.BitmapUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by licheng on 2017/8/3.
 */

public class FaceGridAdapter extends BaseAdapter {

    private List<byte[]> faceList = new ArrayList<>();

    private Context context;

    public FaceGridAdapter(Context context, List<byte[]> faceList) {
        this.context = context;
        this.faceList = faceList;
    }

    @Override
    public int getCount() {
        return faceList.size();
    }

    @Override
    public byte[] getItem(int position) {
        return faceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_face, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final byte[] bmp = faceList.get(position);
        holder.imageView.setImageBitmap(BitmapUtil.convertBmp(BitmapFactory.decodeByteArray(bmp, 0, bmp.length)));
        return convertView;
    }

    private class ViewHolder {
        ImageView imageView;
    }
}
